package com.stu_ques.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 	此工具类 存放所有有关时间格式化方法
 * 
 * */

public class DateUtil
{
	private String timePattern = "yyyy-MM-dd HH:mm:ss";
	private String Pattern = "yyyy-MM-dd";
	/**
	 * 	日期时间转换成字符串	
	 */
	public String dateTimeString(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(timePattern);
		
		return sdf.format(date);
	}
	/**
	 * 	日期转换成字符串
	 * */
	public String dateString(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(Pattern);
		return sdf.format(date);
	}
	/**
	 * 	自定义时间转换成字符串
	 * */
	public String customTime(Date date ,String Pattern)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(Pattern);
		return sdf.format(date);
	}
	/**
	 * 	日期时间转换成字符串	
	 */
	public Date stringToDateTime(String str)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(timePattern);
		try
		{
			return sdf.parse(str);
		} catch (ParseException e)
		{
			//e.printStackTrace();
			System.out.println("=字符串输入的不正确 请按照格式输入=");
		}
		return new Date();
	}
	/**
	 * 	符串转换成字日期
	 * */
	public Date stringToDate(String ster)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(Pattern);
		try
		{
			return sdf.parse(ster);
		} catch (ParseException e)
		{
			//e.printStackTrace();
			System.out.println("=字符串输入的不正确 请按照格式输入=");
		}
		return new Date();
	}
	/**
	 * 	自定义时间转换成字符串
	 * */
	public Date dateToString(String datesrt ,String Pattern)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(Pattern);
		
		try
		{
			return sdf.parse(datesrt);
		} catch (ParseException e)
		{
			//e.printStackTrace();
			System.out.println("=字符串输入的不正确 请按照格式输入=");
		}
		return new Date();
	}
	/**
	 * 	判断是不是昨天
	 *	@param nowDate 结束时间 
	 *	@param oldDate 老时间
	 */
	public boolean isyesDay(Date nowDate , Date oldDate)
	{
		Calendar calendar = Calendar.getInstance() ; 
		/* 将nowDate覆盖当前时间 */
		calendar.setTime(nowDate);
		/* 将时分秒清0 */
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		/* 结束时间
		 * 2018-08-02 00:00:00 */
		Date edDate = calendar.getTime() ; 
		/* 时间-1 */
		calendar.add(Calendar.DATE, -1);
		Date stDate = calendar.getTime() ; 
		return oldDate.after(stDate) && oldDate.before(edDate) ;
	}
}
